package com.wind.trademark.notice.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wind.trademark.common.dao.BaseDao;
import com.wind.trademark.common.vo.PageBean;
import com.wind.trademark.common.vo.PageParam;
import com.wind.trademark.notice.dao.NoticeDao;
import com.wind.trademark.notice.model.NoNotice;
import com.wind.trademark.notice.model.NoReceiveMail;
import com.wind.trademark.notice.model.NoReceiveMessage;
import com.wind.trademark.notice.service.NoticeService;
@Service
public class NoticeServiceImpl implements NoticeService {
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private NoticeDao noticeDao;
	@Override
	public PageBean queryNotice(PageParam pageParam,
			Map<String, Object> params) {
		return noticeDao.queryNotice(pageParam, params);
	}
	@Override
	public NoNotice loadNotice(Long id) {
		return baseDao.get(NoNotice.class, id);
	}
	@Override
	public NoNotice loadNoticeWithoutLazy(Long id) {
		NoNotice noNotice= baseDao.get(NoNotice.class, id);
		baseDao.getHibernateTemplate().initialize(noNotice.getNoReceiveMails());
		baseDao.getHibernateTemplate().initialize(noNotice.getNoReceiveMessages());
		//baseDao.getHibernateTemplate().initialize(noNotice.getNoReceiveSystems());
		return noNotice;
	}
	@Override
	public NoNotice saveOrUpdateNotice(NoNotice noNotice) {
		noNotice.setCreateBy(1L);
		noNotice.setCreateTime(new Date());
		noNotice.setLastUpdateBy(1L);
		noNotice.setLastUpdateTime(new Date());
		baseDao.saveOrUpdate(noNotice);
		noticeDao.deleteNoReceiveMailByNoticeId(noNotice.getId());
		noticeDao.deleteNoReceiveSystemByNoticeId(noNotice.getId());
		noticeDao.deleteNoReceiveMessageByNoticeId(noNotice.getId());
		for(NoReceiveMail noReceiveMail:noNotice.getNoReceiveMails()){
			noReceiveMail.setNoticeId(noNotice.getId());
			baseDao.saveOrUpdate(noReceiveMail);
		}
		for(NoReceiveMessage noReceiveMessage:noNotice.getNoReceiveMessages()){
			noReceiveMessage.setNoticeId(noNotice.getId());
			baseDao.saveOrUpdate(noReceiveMessage);
		}
//		for(NoReceiveSystem noReceiveSystem:noNotice.getNoReceiveSystems()){
//			noReceiveSystem.setNoticeId(noNotice.getId());
//			baseDao.saveOrUpdate(noReceiveSystem);
//		}
		return noNotice;
	}
	@Override
	public void deleteNotices(List<Long> ids) {
		noticeDao.deleteNotices(ids);	
	}
	@Override
	public NoNotice sendNotice(NoNotice noNotice) {
		return this.saveOrUpdateNotice(noNotice);
	}
	@Override
	public List<Map<String, Object>> loadNoticeMobileList() {
		return noticeDao.loadNoticeMobileList();
	}
	@Override
	public List<Map<String, Object>> loadNoticeEmailList() {
		return noticeDao.loadNoticeEmailList();
	}
	
	

}
